﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class ListTinTuc : Form
    {
        public ListTinTuc()
        {
            InitializeComponent();
        }
        private void ListTinTuc_Load(object sender, EventArgs e)
        {
            loadlisttintuc();
        }
        // tu load list tin tuc khi mo form
        private void loadlisttintuc()
        {

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = " select * from TinTuc";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table TINTUC");
            DataTable dt = ds.Tables["Table TINTUC"];

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["Table TINTUC"].DefaultView;
        }
        // load qua from nhap tin tuc
        private void button1_Click(object sender, EventArgs e)
        {
            NhapTinTuc f = new NhapTinTuc();
            if (f.ShowDialog() == DialogResult.OK) 
            {
                loadlisttintuc();
            }

        }
        // update tin tuc
        private void button2_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn sửa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string IDtintuc = dataGridView1.Rows[i].Cells["IDtintuc"].Value.ToString();

                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "update TINTUC set tieude=@tieude,chitiet=@chitiet,ngaydangtin=@ngaydangtin where IDtintuc=@IDtintuc";
                SqlCommand cmd = new SqlCommand(sql, conn);

                cmd.Parameters.AddWithValue("@IDtintuc", IDtintuc);
                cmd.Parameters.AddWithValue("@ngaydangtin", DateTime.Today);
                cmd.Parameters.AddWithValue("@tieude", textBox2.Text);
                cmd.Parameters.AddWithValue("@chitiet", textBox3.Text);
                // cmd.Parameters.AddWithValue("@hinhanh", hinhanh);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                loadlisttintuc();
            }

        }
        // click vao datagridview tu load vào text box và picture box
        private void autoload()
        {
            int i = dataGridView1.SelectedRows[0].Index;
            string IDtintuc = dataGridView1.Rows[i].Cells["IDtintuc"].Value.ToString();

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from TINTUC where IDtintuc= @IDtintuc ";
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.AddWithValue("@IDtintuc", IDtintuc);
            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            while (r.Read())
            {
                textBox1.Text = r["ngaydangtin"].ToString();
                textBox2.Text = r["tieude"].ToString();
                textBox3.Text = r["chitiet"].ToString();
                
                pictureBox1.Image = Image.FromFile((string)r["hinhanh"].ToString());
                filename = (string)r["hinhanh"];
               
            }
            r.Close();
            conn.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            autoload();
        }
        // xoa tin tuc
        private void button3_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string IDtintuc = dataGridView1.Rows[i].Cells["IDtintuc"].Value.ToString();
                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "delete from TINTUC where IDtintuc = @IDtintuc";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDtintuc", IDtintuc);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }

            loadlisttintuc();

        }
        //mở trang lấy hình
        private string filename;
        string tenhinh;
        private void loadhinh()
        {
            OpenFileDialog f = new OpenFileDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                filename = f.FileName;
                tenhinh = System.IO.Path.GetFileName(f.FileName);
                pictureBox1.Image = Image.FromFile(filename);

            }
        }
        // update hình ảnh
        private void updatehinh()
        {
            int i = dataGridView1.SelectedRows[0].Index;
            string IDtintuc = dataGridView1.Rows[i].Cells["IDtintuc"].Value.ToString();

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "update TINTUC set hinhanh=@hinhanh where IDtintuc=@IDtintuc ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@IDtintuc", IDtintuc);
            cmd.Parameters.AddWithValue("@hinhanh", "Img/" + tenhinh);



            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            loadhinh();
            updatehinh();
        }
  
    }
}
